﻿/* **************************************************************************
 *
 * Copyright 2007-2010 Jeff Hardy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * *************************************************************************/

using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Collections.ObjectModel;

namespace NWSGI.HttpListener
{
    public class Context : IWsgiContext
    {
        private IWsgiConfig config;

        private Request request;
        private Response response;
        private ReadOnlyCollection<string> searchPaths;

        private string physicalRoot;
        private string virtualRoot;

        protected HttpListenerContext HttpContext { get; private set; }

        public static string DefaultPhysicalRoot
        {
            get { return Environment.CurrentDirectory; }
        }

        public static string DefaultVirtualRoot
        {
            get { return ""; }
        }

        public Context(HttpListenerContext context, IWsgiConfig config)
            : this(context, config, DefaultPhysicalRoot, DefaultVirtualRoot) { }

        public Context(HttpListenerContext context, IWsgiConfig config, string physicalRoot, string virtualRoot)
        {
            this.HttpContext = context;
            this.config = config;

            this.physicalRoot = physicalRoot;
            this.virtualRoot = virtualRoot.StartsWith("/") ? virtualRoot : "/" + virtualRoot;
        }

        public IWsgiRequest Request
        {
            get { return request ?? (request = GetRequestWrapper()); }
        }

        public IWsgiResponse Response
        {
            get { return response ?? (response = GetResponseWrapper()); }
        }


        public virtual ICollection<string> SearchPaths
        {
            get
            {
                return searchPaths ?? (searchPaths = new ReadOnlyCollection<string>(new[]
                {
                    MapPath("~/"),
                }));
            }
        }

        public virtual string MapPath(string path)
        {
            if(path.StartsWith("~/"))
            {
                return Path.Combine(this.physicalRoot, path.Substring(1).Replace("/", "\\"));
            }

            if(path.StartsWith(this.virtualRoot))
            {
                return Path.Combine(this.physicalRoot, path.Substring(this.virtualRoot.Length).Replace("/", "\\"));
            }

            return path;
        }

        protected virtual Request GetRequestWrapper()
        {
            return new Request(this.HttpContext.Request);
        }

        protected virtual Response GetResponseWrapper()
        {
            return new Response(this.HttpContext.Response);
        }
    }
}
